﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BonSaiTableAdapters;

public partial class admin_ProductDetail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) 
        {
            if (Request.QueryString["productID"] != null)
            {
                try
                {
                    int productID = Int32.Parse(Request.QueryString["productID"].ToString());
                    ViewState["productID"] = productID;
                    LoadProductInfo();

                }
                catch (Exception ex)
                {

                }

            }
            else
            {
                btnUpdate.Text = "Tạo mới";
                btnDelete.Visible = false;
            }
        }
    }
    private void LoadProductInfo() 
    {
        if (ViewState["productID"] != null) 
        {
            int productID = Int32.Parse(ViewState["productID"].ToString());
            ProductsTableAdapter productDA = new ProductsTableAdapter();
            BonSai.ProductsDataTable productDT = productDA.GetProductByProductID(productID);
            if (productDT.Rows.Count > 0) 
            {
                BonSai.ProductsRow productRow = productDT.Rows[0] as BonSai.ProductsRow;
                txtProductID.Text = productRow.ProductID.ToString();
                if (!productRow.IsProductNameNull())
                    txtProductName.Text = productRow.ProductName;
                if (!productRow.IsBriefDescNull())
                    //fckBrief.Value = productRow.BriefDesc;
                if (!productRow.IsOldPriceNull()) 
                    txtOldPrice.Text = productRow.OldPrice.ToString();
                if (!productRow.IsSalePriceNull())
                    txtSalePrice.Text = productRow.SalePrice.ToString();
                hlRepresentImage.Visible = true;
                hlRepresentImage.NavigateUrl = "Product_RepresentImage.aspx?productID=" + productID;
                if (!productRow.IsRepresentImageNull()) 
                {
                    image.ImageUrl = "~/img_product/" + productRow.RepresentImage;
                }
                ddlQuantity.SelectedValue = productRow.Quantity.ToString();
                ddlPaceOfOrgrin.SelectedValue = productRow.PlaceID.ToString();
                
                ddlStatus.SelectedValue = productRow.Status == true ? "true" : "false";
            }
            
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("ProductManage.aspx");
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (!Page.IsValid)
            return;

        string productName = txtProductName.Text.Trim();
        //string briefDesc = fckBrief.Value;
        string briefDesc = "";
        decimal oldPrice = Decimal.Parse(txtOldPrice.Text);
        decimal salePrice = Decimal.Parse(txtSalePrice.Text);
        int categoryID = Int32.Parse(ddlCategory.SelectedValue);
        int placeID = Int32.Parse(ddlPaceOfOrgrin.SelectedValue);
        int quantity = Int32.Parse(ddlQuantity.SelectedValue);
        bool status = Boolean.Parse(ddlStatus.SelectedValue);
        

        ProductsTableAdapter productDA = new ProductsTableAdapter();
        if (ViewState["productID"] != null)
        {
            ///UpdateProduct
            int productID = Int32.Parse(ViewState["productID"].ToString());
            productDA.Update(productName, categoryID, briefDesc, oldPrice, salePrice, quantity, status, "Green", "Nhiet Doi Am", placeID, "", productID);
            Response.Redirect("ProductManage.aspx?code=2");
        }
        else
        { 
            //Add Product
            productDA.Insert(productName, categoryID,briefDesc, oldPrice, salePrice, quantity, status, "Green", "Climate", placeID, "");
            Response.Redirect("ProductManage.aspx?code=1");
        }
        
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
       
        if (ViewState["productID"] != null) 
        {
            int productID = Int32.Parse(ViewState["productID"].ToString());

            OrderDetailsTableAdapter orderDetailDA = new OrderDetailsTableAdapter();
            BonSai.OrderDetailsDataTable orderDetailDT = orderDetailDA.GetOrderDetailByProductID(productID);
            if (orderDetailDT.Rows.Count > 0) 
            {
                lblMessage.Text = "Bạn không thể xóa sản phẩm này !";
            }
            else
            {
                ProductsTableAdapter productDA = new ProductsTableAdapter();
                productDA.Delete(productID);
                Response.Redirect("ProductManage.aspx?code=3");

                
            }
        }
    }
}
